home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / EXAMPLES / IDRAW / LIST.C < prev    next >
C/C++ Source or Header  |  1980-01-05  |  5KB  |  185 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: list.c,v 1.9 89/10/09 14:48:32 linton Exp $
  24. // implements classes BaseNode and BaseList.
  25.  
  26. #include "list.h"
  27.  
  28. // BaseNode zeroes its pointers.
  29.  
  30. BaseNode::BaseNode () {
  31.     prev = nil;
  32.     next = nil;
  33. }
  34.  
  35. BaseNode::~BaseNode () {
  36.     // no storage allocated by base class
  37. }
  38.  
  39. // SameValueAs returns true if this class contains a data member of
  40. // any pointer type that has the same value as the given pointer.
  41.  
  42. boolean BaseNode::SameValueAs (void*) {
  43.     // base class contains no data members
  44.     return false;
  45. }
  46.  
  47. // BaseList starts with only a header node.
  48.  
  49. BaseList::BaseList () {
  50.     cur = head = new BaseNode;
  51.     head->prev = head->next = head;
  52.     size = 0;
  53. }
  54.  
  55. // ~BaseList destroys the list.
  56.  
  57. BaseList::~BaseList () {
  58.     DeleteAll();
  59.     delete head;
  60. }
  61.  
  62. // Index returns the node that would be the indexed element if the
  63. // list was a C array (0 = first, 1 = next, ..., size-1 = last) or nil
  64. // if the index is out of bounds.  Index sets the current node to the
  65. // indexed node if it's in the list; otherwise, the current node
  66. // remains the same.
  67.  
  68. BaseNode* BaseList::Index (int index) {
  69.     BaseNode* elem = nil;
  70.     if (index >= 0 && index < size) {
  71.     elem = head->next;
  72.     for (int i = 0; i < index; i++) {
  73.         elem = elem->next;
  74.     }
  75.     cur = elem;
  76.     }
  77.     return elem;
  78. }
  79.  
  80. // Find returns true if the list contains a node with a data member
  81. // whose value is the same as the given pointer or false if there is
  82. // no such node.  Find sets the current node to that node only if it
  83. // finds such a node; otherwise, the current node remains the same.
  84.  
  85. boolean BaseList::Find (void* value) {
  86.     for (BaseNode* elem = head->next; elem != head; elem = elem->next) {
  87.     if (elem->SameValueAs(value)) {
  88.         cur = elem;
  89.         return true;
  90.     }
  91.     }
  92.     return false;
  93. }
  94.  
  95. // Append inserts a node after the last node of the list.  The current
  96. // node remains the same.
  97.  
  98. void BaseList::Append (BaseNode* appendee) {
  99.     BaseNode* last = head->prev;
  100.     appendee->prev = last;
  101.     appendee->next = head;
  102.     head->prev = appendee;
  103.     last->next = appendee;
  104.     ++size;
  105. }
  106.  
  107. // Prepend inserts a node before the first node of the list.  The
  108. // current node remains the same.
  109.  
  110. void BaseList::Prepend (BaseNode* prependee) {
  111.     BaseNode* first = head->next;
  112.     prependee->prev = head;
  113.     prependee->next = first;
  114.     first->prev = prependee;
  115.     head->next  = prependee;
  116.     ++size;
  117. }
  118.  
  119. // InsertAfterCur inserts a node after the current node of the list.
  120. // The current node remains the same.
  121.  
  122. void BaseList::InsertAfterCur (BaseNode* prependee) {
  123.     BaseNode* first = cur->next;
  124.     prependee->prev = cur;
  125.     prependee->next = first;
  126.     first->prev = prependee;
  127.     cur->next   = prependee;
  128.     ++size;
  129. }
  130.  
  131. // InsertBeforeCur inserts a node before the current node of the list.
  132. // The current node remains the same.
  133.  
  134. void BaseList::InsertBeforeCur (BaseNode* appendee) {
  135.     BaseNode* last = cur->prev;
  136.     appendee->prev = last;
  137.     appendee->next = cur;
  138.     cur->prev  = appendee;
  139.     last->next = appendee;
  140.     ++size;
  141. }
  142.  
  143. // RemoveCur removes the current node of the list (if it's a node and
  144. // not the head) and sets the current node to the following node.
  145.  
  146. void BaseList::RemoveCur () {
  147.     if (cur != head) {
  148.     BaseNode* before = cur->prev;
  149.     BaseNode* after  = cur->next;
  150.     after->prev  = before;
  151.     before->next = after;
  152.     cur = after;
  153.     --size;
  154.     }
  155. }
  156.  
  157. // DeleteCur deletes the current node of the list (if it's a node and
  158. // not the head) and sets the current node to the following node.
  159.  
  160. void BaseList::DeleteCur () {
  161.     if (cur != head) {
  162.     BaseNode* before = cur->prev;
  163.     BaseNode* after  = cur->next;
  164.     after->prev  = before;
  165.     before->next = after;
  166.     delete cur;
  167.     cur = after;
  168.     --size;
  169.     }
  170. }
  171.  
  172. // DeleteAll deletes all the nodes of the list, making the list empty
  173. // again, and sets the current node to the list's head.
  174.  
  175. void BaseList::DeleteAll () {
  176.     BaseNode* after = nil;
  177.     for (BaseNode* doomed = head->next; doomed != head; doomed = after) {
  178.     after = doomed->next;
  179.     delete doomed;
  180.     }
  181.     cur = head;
  182.     head->prev = head->next = head;
  183.     size = 0;
  184. }
  185.